import logging

import allure
import jsonpath

from utils.send_request_utils import send_jdbc_query


# json提取
def json_extractor(case,res,all,index=0):
    if case["jsonExData"]:
        with allure.step("4: JSON提取"):
            for key, value in eval(case["jsonExData"]).items():
                jsonExData = jsonpath.jsonpath(res.json(), value)[index]
                logging.info(f"4.JSON提取，根据({case['jsonExData']})提取数据，提取的数据为({jsonExData}),此时全局变量为：({all})")
                allure.attach(f"({case['jsonExData']})的提取值为 ({jsonExData})", name="JSON提取结果")
                all[key] = jsonExData

# 数据库提取
def jdbc_extractor(case,all):
    if case["sqlExData"]:
        with allure.step("4: JDBC提取"):
            for key, value in eval(case["sqlExData"]).items():
                query_data = send_jdbc_query(value)
                logging.info(f"4.JDBC提取，根据({case['sqlExData']})提取数据，提取的数据为({query_data}),此时全局变量为：({all})")
                allure.attach(f"({case['sqlExData']})的提取值为 ({query_data})", name="JDBC提取结果")
                all[key] = query_data
